package com.cx.Controller.back.api;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cx.entity.Article;
import com.cx.service.impl.ArticleServiceImpl;
import com.cx.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cx
 * @since 2024-06-07
 */
@CrossOrigin    //解决跨域问题注解
@RestController
@RequestMapping("/back-article")
public class BackArticleController {
    //配置基于权重的轮询算法
    @Autowired
    private ArticleServiceImpl articleService;

    //        注意：lay-ui成功code为0
//        查询全部文章信息
    @RequestMapping("list")
    public R getArticleAll(@RequestParam Map<String, Object> params) {
        System.out.println("params = " + params);
        IPage<Article> articleIPage = articleService.getArticleListPageConditions(params);
        return R.ok(0, "文章数据请求成功").put("count", articleIPage.getTotal()).put("data", articleIPage.getRecords());
    }

    //    文章添加
    @RequestMapping("save")
    public R articleSave(Article article) {
        return R.ok().put("data", articleService.save(article));
    }

    //    文章修改
    @RequestMapping("update")
    public R articleUpdateById(Article article) {
        return R.ok().put("data", articleService.updateById(article));
    }

    //    文章删除
    @RequestMapping("del")
    public R delete(@RequestBody Integer[] ids) {//[1,2]
        articleService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
